package com.example.demo.algorithm.lesson.basic.class2;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author liweizhi
 * @date 2020/4/27
 */
public class TwoQueueImplementStack<E> {
    Queue<E> queue = new LinkedList<>();
    Queue<E> helper = new LinkedList<>();

    public void push(E e) {
        queue.offer(e);
    }

    public E pop() {
        if (queue.isEmpty()) {
            throw new RuntimeException("empty stack!");
        }
        while (queue.size() > 1) {
            helper.offer(queue.poll());
        }
        E pop = queue.poll();
        Queue<E> t = queue;
        queue = helper;
        helper = t;
        return pop;
    }

    public static void main(String[] args) {
        TwoQueueImplementStack<Integer> stack = new TwoQueueImplementStack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}
